'use strict';
// ----------------------  Iterator All Posible Inputs ----------------------
// const rst = [];
let found = false, max_width = 0, max_step = 0, has_trip = 0;
function iteratorTest(size) {
  (async () => {
    for (let ri = 0; ri < Math.pow(2, size); ri++) {
      for (let ti = 0; ti < Math.pow(3, size); ti++) {
        await (async () => {
          let rs = ri.toString(2).padStart(size, '0'),
            ts = ti.toString(3).padStart(size, '0'),
            cnt = 0,
            wth = 0;

          // console.log('oneTrip ri %d, ti %d , rs %s , ts %s ', ri, ti, rs, ts);

          robots = Array.from(rs).map(e => +e);
          track = Array.from(ts).map(e => +e);

          while (robots.some(e => !!e) || Number.parseInt(track.join(''), 3) >= 2) {
            trip();
            cnt++;
            wth = Math.max(track.length, wth);
            if (cnt > 10000) {
              found = true;
              break;
            }
          }
          // rst.push({ R: rs, B: ts, T: cnt, L: wth });

          if (max_step < cnt) {
            max_step = cnt;
            console.log('MAX STEP RENEW', { R: rs, B: ts, T: cnt, L: wth });
          }

          if (max_width < wth) {
            max_width = wth
            console.log('MAX WIDTH RENEW', { R: rs, B: ts, T: cnt, L: wth });
          }

          has_trip++;
          return;
        })();

        if (found) break;
      }

      if (found) break;
    }

    console.log('has_trip', has_trip);
  })()
}

// fillZero = false;
// iteratorTest(3);

/* size 3, trip 216: {R: "001", B: "221", T: 123, L: 6}               */
/* size 4, trip 1296: {R: "1011", B: "2101", T: 180, L: 7}            */
/* size 5, trip 7776: {R: "00101", B: "22111", T: 260, L: 8}          */
/* size 6, trip 46656: {R: "011001", B: "211011", T: 322, L: 10}      */
/* size 7, trip 279936: {R: "0111001", B: "2212211", T: 441, L: 11}   */
/* size 8 : {R: "10100011", B: "21001201", T: 529, L: 15}             */